<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}角色`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="drawerProps.row!.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色说明" prop="note">
        <el-input type="textarea" :rows="5" v-model="drawerProps.row!.note" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色KEY" prop="key">
        <el-input v-model="drawerProps.row!.key" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, ElNotification, FormInstance } from "element-plus";
import { Roles } from "@/api/types/base";
import { useUserStore } from "@/stores/modules/user";
import apiService from "@/api/services/apiService";

const userStore = useUserStore();
const rules = reactive({
  name: [{ required: true, message: "请输入组织名" }]
});

interface DrawerProps {
  title: string;
  tbName: string;
  isView: boolean;
  row: Partial<Roles>;
  api?: (tabName: string, params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  tbName: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 查重
const checkRepeat = async (): Promise<boolean> => {
  let condition = {
    name: drawerProps.value.row.name
  };
  if (drawerProps.value.row._id) {
    condition["_id"] = { $ne: drawerProps.value.row._id };
  }
  let { data } = await apiService.findCount<number>(drawerProps.value.tbName, condition);
  return data > 0;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      if (await checkRepeat()) {
        ElNotification({
          title: "温馨提示",
          message: `角色重名重复，请重新输入。`,
          type: "warning"
        });
        return;
      }
      drawerProps.value.row.createUser = userStore.userInfo.userName;
      drawerProps.value.row.createDate = new Date();
      await drawerProps.value.api!(drawerProps.value.tbName, drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
@/api/types
